package com.thudojsc.canvasengine;

import android.content.Context;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.WindowManager;

public class ResolutionDetector {
	/*-------------------LOAI DO PHAN GIAI---------------------*/
	//Thong so de danh gia la tri so max cua cao x rong
	//Thong so nay duoc goi la max_demension
	public final static int RESOLUTION_LOW = 0; //240x320 max_demension < 480
	public final static int RESOLUTION_MEDIUM = 1; //320x480 480x800  480<= max_demension <= 800
	public final static int RESOLUTION_HIGH = 2; //max_demension > 800
	
	private static int mScreenWidth = 0;
	private static int mScreenHeight = 0;
	
	private static float mDensity = 0;
	private static float mScaledDensity = 0;
	
	private static DisplayMetrics mDisplayMetrics;
	
	public static void detect(final Context pContext) {
		final WindowManager windowManager = (WindowManager) pContext.getSystemService(Context.WINDOW_SERVICE);
		
		final DisplayMetrics metrics = new DisplayMetrics();
		
		windowManager.getDefaultDisplay().getMetrics(metrics);
		
		mScreenWidth = metrics.widthPixels;
		mScreenHeight = metrics.heightPixels;
		mDensity = metrics.density;
		mScaledDensity = metrics.scaledDensity;
		mDisplayMetrics = metrics;
	}
	
	public static int getScreenWidth() {
		return mScreenWidth;
	}
	
	public static int getScreenHeight() {
		return mScreenHeight;
	}
	
	public static int getResolutionType() {
		final int max_demension = Math.max(mScreenWidth, mScreenHeight);
		
		if (max_demension < 480) 
			return RESOLUTION_LOW;
		else 
		if (max_demension <= 800 && max_demension >= 480)
			return RESOLUTION_MEDIUM;
		else
			return RESOLUTION_HIGH;
	}
	
	public static float getDensity() {
		return mDensity;
	}
	
	public static float getScaledDensity() {
		return mScaledDensity;
	}
	
	public static int convertSpToDp(final float sp) {
		return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, mDisplayMetrics);
	}
	
	public static int convertDpToPx(final float dp) {
		return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, mDisplayMetrics);
	}
}
